<?php

namespace app\admin\controller;

use think\facade\Config;
use app\model\order\OrderCommon as OrderCommonModel;


class Mrnaorder extends BaseAdmin
{

    public function lists()
    {
        $order_label_list = array(
            "order_no" => "订单号",
            "out_trade_no" => "外部单号",
//            "name" => "收货人姓名",
//            "mobile" => "收货人手机号",
            "order_name" => "商品名称",
            "site_name" => "店铺名称",
        );
        $order_status = input("order_status", "");//订单状态
        $order_name = input("order_name", '');
        $pay_type = input("pay_type", '');
        $order_from = input("order_from", '');
        $start_time = input("start_time", '');
        $end_time = input("end_time", '');
        $site_id = input("site_id", "");
        $order_label = !empty($order_label_list[input("order_label")]) ? input("order_label") : "";
        $search_text = input("search", '');
        $promotion_type = input("promotion_type", '');
        $order_type = input("order_type", 'all');//营销类型
        $is_own = input('is_own','');
        $settlement_state = input("settlement_state", "");//结算状态
        $order_common_model = new OrderCommonModel();
        if (request()->isAjax()) {
            $page_index = input('page', 1);
            $page_size = input('limit', PAGE_LIST_ROWS);
            $alias = 'o';
            $join = null;
            $condition = [
                ["order_type", "=", 3],
                ["is_mrna", "=", 1]
            ];
            //订单状态
            if ($order_status != "") {
                if($order_status != 'refunding'){
                    $condition[] = [ "o.order_status", "=", $order_status ];
                }else{
                    $join = [
                        [
                            'order_goods og',
                            'og.order_id = o.order_id',
                            'left'
                        ]
                    ];
                    $condition[] = [ "og.refund_status", "not in", [0,3] ];
                }
            }
            //是否自营
            if($is_own !== ''){
                $join = [
                    [
                        'shop s',
                        's.site_id = o.site_id',
                        'left'
                    ]
                ];
                $condition[] = ['s.is_own','=',$is_own];
            }
            //订单内容 模糊查询
            if ($order_name != "") {
                $condition[] = ["o.order_name", 'like', "%$order_name%"];
            }
            //订单来源
            if ($order_from != "") {
                $condition[] = ["o.order_from", "=", $order_from];
            }
            //订单支付
            if ($pay_type != "") {
                $condition[] = ["o.pay_type", "=", $pay_type];
            }
            //订单类型
            if($order_type != 'all'){
                $condition[] = ["o.order_type", "=", $order_type];
            }
            //结算状态
            if ($settlement_state ==1) {
                $condition[] = ["o.is_settlement", "=", '1'];
            }elseif($settlement_state==2){
                $condition[] = ["o.is_settlement", "=", '0'];
            }
            //营销类型
            if ($promotion_type != "") {
                if($promotion_type == 'empty'){
                    $condition[] = ["o.promotion_type", "=", ''];
                }else{
                    $condition[] = ["o.promotion_type", "=", $promotion_type];
                }
            }
            if (!empty($start_time) && empty($end_time)) {
                $condition[] = ["o.create_time", ">=", date_to_time($start_time)];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = ["o.create_time", "<=", date_to_time($end_time)];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ 'o.create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            }
            if ($search_text != "") {
                $condition[] = ['o.'.$order_label, 'like', "%$search_text%"];
            }
            if(!empty($site_id))
            {
                $condition[] = ["o.site_id", '=', $site_id];
            }
            $list = $order_common_model->getOrderPageList($condition, $page_index, $page_size, "o.create_time desc", $field = 'o.*', $alias, $join);

            foreach ($list['data']['list'] as $k=>$v){
                $mrna_name = '';
                if ($v['mrna_member_id']){
                    $mrna = model('member')->getInfo(['member_id'=>$v['mrna_member_id']],'nickname');

                    $mrna_name = $mrna['nickname'];
                }
                $list['data']['list'][$k]['mrna_member_name'] = $mrna_name;

                $member = model('member')->getInfo(['member_id'=>$v['member_id']],'nickname,mobile');
                $list['data']['list'][$k]['member_info'] = $member;
            }
            return $list;
        } else {
            $order_type_list = $order_common_model->getOrderTypeStatusList();
            //dump($order_type_list);die;
            $this->assign("order_type_list", $order_type_list);
            $this->assign("order_label_list", $order_label_list);
            $this->assign("order_status_list", $order_type_list[3]['status']);//订单状态
            //dump($order_type_list);die;
            //订单来源 (支持端口)
            $order_from = Config::get("app_type");
            $this->assign('order_from_list', $order_from);

            $pay_type = $order_common_model->getPayType();
            $this->assign("pay_type_list", $pay_type);

            //营销活动类型
            $order_promotion_type = event('OrderPromotionType');
            $this->assign("promotion_type", $order_promotion_type);
            $this->assign("http_type", get_http_type());
            return $this->fetch('mrnaorder/lists');
        }

    }

    /**
     * 外卖订单详情
     */
    public function detail()
    {
        $order_id = input("order_id", 0);
        $order_common_model = new OrderCommonModel();
        $condition = array(
            ['order_id', '=', $order_id],
        );
        $order_detail_result = $order_common_model->getOrderDetail($condition);
        $order_detail = $order_detail_result[ "data" ];
        $this->assign("order_detail", $order_detail);
        return $this->fetch('mrnaorder/detail');
    }
}